﻿using System.Collections;
using Assets.AnimationPlayer.Scripts.Util;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Assets.AnimationPlayer.Scripts.Prefabs
{
    public class MyScrollRect : ScrollRect
    {
        #region 属性列表
        //是否正在滑动标记
        public bool IsScrolling;

        public float InitOffsetX;
        public float InitOffsetY;
        #endregion

        protected override void Start()
        {
            base.Start();
            movementType = MovementType.Clamped;
            Refresh();
        }

        //刷新
        public void Refresh()
        {
            foreach (Transform tf in content)
            {
                _bindItemEvents(tf);
                //_bindSubItemEvents(tf);
            }
            _resetScroll();
        }

        private void _bindSubItemEvents(Transform parentTransform)
        {
            foreach (Transform tf in parentTransform)
            {
                _bindItemEvents(tf);
                _bindSubItemEvents(tf);
            }
        }


        //重置滑动
        public void _resetScroll()
        {
            _refreshScrollBar();
            //content.offsetMin = new Vector2(InitOffsetX, InitOffsetY);
        }

        //更新能否滑动
        public void _refreshScrollBar()
        {
            vertical = transform.GetComponent<RectTransform>().rect.height < content.rect.height;
            horizontal = transform.GetComponent<RectTransform>().rect.width < content.rect.width;
        }

        #region Private-Events
        //绑定滑动事件
        private void _bindItemEvents(Transform tf)
        {
            YioksTriggerListener.Get(tf).OnDragBegin = ItemDragBegin;
            YioksTriggerListener.Get(tf).OnDraging = ItemDrag;
            YioksTriggerListener.Get(tf).OnDragEnd = ItemDragEnd;
            YioksTriggerListener.Get(tf).OnDown = OnPointerDown;
            YioksTriggerListener.Get(tf).OnUp = OnPointerUp;
        }

        private static void OnPointerUp(GameObject go)
        {

        }

        private static void OnPointerDown(GameObject go)
        {

        }

        //开始滑动
        private void ItemDragBegin(GameObject go, PointerEventData eventData)
        {
            IsScrolling = true;
            OnBeginDrag(eventData);
        }
        //滑动
        private void ItemDrag(GameObject go, PointerEventData eventData)
        {
            OnDrag(eventData);
        }
        //结束滑动
        private void ItemDragEnd(GameObject go, PointerEventData eventData)
        {
            OnEndDrag(eventData);
            StartCoroutine(_stopScrolling());
            //IsScrolling = false;
        }

        private IEnumerator _stopScrolling()
        {
            yield return new WaitForEndOfFrame();
            IsScrolling = false;

        }

        //滑到顶端
        public void ScrollTop()
        {
            //ContentRtf.anchoredPosition = new Vector2(ContentRtf.anchoredPosition.x, 0);
            _refreshScrollBar();
            //this.verticalScrollbar.value = !vertical ? 0 : 1;
            content.anchoredPosition = new Vector2(content.anchoredPosition.x, 0);
        }


        //滑到底端
        public void ScrollBottom()
        {
            _refreshScrollBar();
            //this.verticalScrollbar.value = !vertical ? 1 : 0;
            var parentRect = transform.GetComponent<RectTransform>();
            content.anchoredPosition = vertical ? new Vector2(content.anchoredPosition.x, Mathf.Min(0, parentRect.rect.height - content.rect.height)) : new Vector2(content.anchoredPosition.x, 0);
        }

        //滑到底端
        public void ScrollBottomOrCenter()
        {
            _refreshScrollBar();
            if (vertical)
            {
                content.anchorMin = new Vector2(content.anchorMin.x, 1);
                content.anchorMax = new Vector2(content.anchorMax.x, 1);
                content.pivot = new Vector2(content.pivot.x, 0);
                var parentRect = transform.GetComponent<RectTransform>();
                content.anchoredPosition = new Vector2(content.anchoredPosition.x, -parentRect.rect.height);
            }
            else
            {
                content.anchorMin = new Vector2(content.anchorMin.x, 0.5f);
                content.anchorMax = new Vector2(content.anchorMax.x, 0.5f);
                content.pivot = new Vector2(content.pivot.x, 0.5f);
                content.anchoredPosition = new Vector2(content.anchoredPosition.x, 0);
            }
        }

        //滑到左侧
        public void ScrollLeft(float x = 0)
        {
            _refreshScrollBar();
            //this.horizontalScrollbar.value = !horizontal ? 0 : 1;
            content.anchoredPosition = new Vector2(x, content.anchoredPosition.y);
        }

        //滑到右侧
        public void ScrollRight()
        {
            _refreshScrollBar();

            var parentRect = transform.GetComponent<RectTransform>();
            content.anchoredPosition = horizontal ? new Vector2(Mathf.Min(0, parentRect.rect.width - content.rect.width), content.anchoredPosition.y) : new Vector2(0, content.anchoredPosition.y);

        }
        #endregion
    }
}

